/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.surfmanager.ws;

import com.google.gson.Gson;
import com.surfmanager.dao.ProfileDAO;
import com.surfmanager.dao.SurfSessionDAO;
import com.surfmanager.dao.TripDAO;
import com.surfmanager.model.Profile;
import com.surfmanager.model.SurfSession;
import com.surfmanager.model.SurfSessionFilter;
import com.surfmanager.model.Trip;
import com.surfmanager.model.TripFilter;
import com.surfmanager.model.result.CodeType;
import com.surfmanager.model.result.ProfileResult;
import com.surfmanager.model.result.Result;
import com.surfmanager.model.result.SurfSessionResult;
import com.surfmanager.model.result.TripResult;
import com.surfmanager.util.Utilities;
import java.util.Set;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author eamiker
 */
@WebService(serviceName = "SurfWS")
public class SurfWS {

    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "addProfile")
    public String addProfile(@WebParam(name = "userName") String userName, @WebParam(name = "password") String password, @WebParam(name = "email") String email, @WebParam(name = "firstName") String firstName, @WebParam(name = "lastName") String lastName, @WebParam(name = "age") int age, @WebParam(name = "phone") String phone) {
       
         Profile profile = new Profile(firstName, lastName, userName, age, password, email, phone);
        ProfileDAO profileDao = new ProfileDAO();
        CodeType code = profileDao.save(profile);
       Result result = new Result((Profile)profile, code);
        
       Gson gson = new Gson();
       return gson.toJson(result);
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getProfile")
    public String getProfile(@WebParam(name = "userName") String userName) {
        try{
        ProfileDAO profileDao = new ProfileDAO();
        Profile profile = profileDao.getProfile(userName);
        
        ProfileResult profileResult = Utilities.convertProfile(profile);
        
        Result result;
        if(profile!=null){
             result = new Result((ProfileResult)profileResult, CodeType.OK);
        }else{
             result = new Result((ProfileResult)profileResult, CodeType.KO_INEXISTING_PROFILE);
        }
       Gson gson = new Gson();
        return gson.toJson(result);
         //return result.toString();
        }catch(Exception ex){
                   System.err.println("error Amine:"+ex.getStackTrace()+"\n"+ex.getMessage()+"\n"+ex.getCause());
             }
        return null;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "createSurfSession")
    public String createSurfSession(@WebParam(name = "userName") String userName, @WebParam(name = "departDate") String departDate, @WebParam(name = "arrivalDate") String arrivalDate,@WebParam(name = "departPlace") String departPlace, @WebParam(name = "sessionPlace") String sessionPlace, @WebParam(name = "sessionDuration") String sessionDuration, @WebParam(name = "sessionProbability") Double sessionProbability, @WebParam(name = "maxParticipants") Integer maxParticipants, @WebParam(name = "pricePerParticipant") Double pricePerParticipant, @WebParam(name = "transport") String transport, @WebParam(name = "description") String description) {
         SurfSessionDAO surfSessionDao = new SurfSessionDAO();
        
         
           SurfSession surfSession = new SurfSession(Utilities.getDateFromString(departDate), Utilities.getDateFromString(arrivalDate), departPlace, sessionPlace, sessionDuration, sessionProbability, maxParticipants, pricePerParticipant, transport, description);
        
           CodeType code = surfSessionDao.createSurfSession(userName, surfSession);
           
          Result result = new Result(Utilities.convertSession(surfSession), code);
         Gson gson = new Gson();
        return gson.toJson(result);
    }

    
    /**
     * Web service operation
     */
    @WebMethod(operationName = "getSurfSessionByFilter")
    public String getSurfSessionByFilter(@WebParam(name = "userName") String userName, @WebParam(name = "departDate") String departDate, @WebParam(name = "arrivalDate") String arrivalDate,@WebParam(name = "departPlace") String departPlace, @WebParam(name = "sessionPlace") String sessionPlace, @WebParam(name = "sessionDuration") String sessionDuration, @WebParam(name = "sessionProbability") Double sessionProbability, @WebParam(name = "maxParticipants") Integer maxParticipants, @WebParam(name = "pricePerParticipant") Double pricePerParticipant, @WebParam(name = "transport") String transport) {
        try{
        SurfSessionDAO surfSessionDao = new SurfSessionDAO();
         
        SurfSessionFilter filter = new SurfSessionFilter(Utilities.getDateFromString(departDate), Utilities.getDateFromString(arrivalDate), departPlace, sessionPlace, sessionDuration, sessionProbability, maxParticipants, pricePerParticipant, transport, userName);
           
        Set<SurfSession> surfSessionSet = surfSessionDao.getSurfSessionByFilter(filter);
        
        Set<SurfSessionResult> surfSessionsResult = Utilities.convertSessions(surfSessionSet);
        
        Result result;
        if(surfSessionsResult!=null){
             result = new Result((Set<SurfSessionResult>) surfSessionsResult, CodeType.OK);
        }else{
             result = new Result((Set<SurfSessionResult>) surfSessionsResult, CodeType.KO_UNDEFINED);
        }
       Gson gson = new Gson();
        return gson.toJson(result);
        }catch(Exception ex){
                   System.err.println("error:"+ex.getStackTrace()+"\n"+ex.getMessage()+"\n"+ex.getCause());
             }
        return null;
    }
    
    
    /**
     * Web service operation
     */
    @WebMethod(operationName = "createTrip")
    public String createTrip(@WebParam(name = "userName") String userName, @WebParam(name = "departDate") String departDate, @WebParam(name = "arrivalDate") String arrivalDate,@WebParam(name = "departPlace") String departPlace, @WebParam(name = "tripPlace") String tripPlace, @WebParam(name = "tripDuration") String tripDuration, @WebParam(name = "tripProbability") Double tripProbability, @WebParam(name = "maxParticipants") Integer maxParticipants, @WebParam(name = "pricePerParticipant") Double pricePerParticipant, @WebParam(name = "transport") String transport, @WebParam(name = "description") String description) {
         TripDAO tripDao = new TripDAO();
        Trip trip = new Trip(Utilities.getDateFromString(departDate), Utilities.getDateFromString(arrivalDate), departPlace, tripPlace, tripDuration, tripProbability, maxParticipants, pricePerParticipant, transport, description);
        CodeType code = tripDao.createTrip(userName, trip);
           
          Result result = new Result(Utilities.convertTrip(trip), code);
         Gson gson = new Gson();
        return gson.toJson(result);
    }

    
    /**
     * Web service operation
     */
    @WebMethod(operationName = "getTripByFilter")
    public String getTripByFilter(@WebParam(name = "userName") String userName, @WebParam(name = "departDate") String departDate, @WebParam(name = "arrivalDate") String arrivalDate,@WebParam(name = "departPlace") String departPlace, @WebParam(name = "tripPlace") String tripPlace, @WebParam(name = "tripDuration") String tripDuration, @WebParam(name = "tripProbability") Double tripProbability, @WebParam(name = "maxParticipants") Integer maxParticipants, @WebParam(name = "pricePerParticipant") Double pricePerParticipant, @WebParam(name = "transport") String transport) {
        try{
        TripDAO tripDao = new TripDAO();
         
        TripFilter filter = new TripFilter(Utilities.getDateFromString(departDate), Utilities.getDateFromString(arrivalDate), departPlace, tripPlace, tripDuration, tripProbability, maxParticipants, pricePerParticipant, transport, userName);
           
          Set<Trip> tripSet = tripDao.getTripByFilter(filter);
        
        Set<TripResult> tripsResult = Utilities.convertTrips(tripSet);
        
        Result result;
        if(tripsResult!=null){
             result = new Result((Set<TripResult>) tripsResult, CodeType.OK);
        }else{
             result = new Result((Set<TripResult>) tripsResult, CodeType.KO_UNDEFINED);
        }
       Gson gson = new Gson();
        return gson.toJson(result);
        }catch(Exception ex){
                   System.err.println("error:"+ex.getStackTrace()+"\n"+ex.getMessage()+"\n"+ex.getCause());
             }
        return null;
    }
    
    
}
